package com.yzcrm.erp.crm.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.paragetter.Para;
import com.yzcrm.common.annotation.Permissions;
import com.yzcrm.common.config.paragetter.BasePageRequest;
import com.yzcrm.erp.crm.service.CrmQuotationCombinationService;
import com.yzcrm.swagger.annotation.ActionApi;
import com.yzcrm.swagger.annotation.Api;
import com.yzcrm.swagger.annotation.Param;
import com.yzcrm.swagger.annotation.Params;
import org.apache.poi.ss.formula.functions.T;

/**
 * Flowers bloom all over the ground, frequent urination urination not enough!
 * IDE: Created with by IntelliJ IDEA.
 * program: yzcrm
 *
 * @author :jones
 * github: https://github.com/luckyrxp
 * email: jonesxr@163.com
 * version: v 1.0
 *
 * <p>
 * <p>
 * date: 2020-08-22
 * time: 12:00
 * description:报价列表 controller
 * Copyright(©) 2020 by Jones
 **/
@Api(remark = "报价：报价列表相关")
public class CrmQuotationCombinationController extends Controller {

    @Inject
    CrmQuotationCombinationService combinationService;


    /**
     * 查看报价列表页
     *
     * @param basePageRequest basePageRequest
     */
    @Permissions({"crm:quotation:index"})
    @ActionApi(summary = "检索报价列表数据", remark = "检索报价列表数据")
    @Params({
            @Param(name = "sceneId", remark = "场景Id", dataType = "int"),
            @Param(name = "businessId", remark = "商机名称", dataType = "String"),
            @Param(name = "type", remark = "报价列表type值从750起", dataType = "String", required = true)
    })
    public void queryPageList(BasePageRequest<T> basePageRequest) {
        renderJson(combinationService.filterConditionAndGetPageList(basePageRequest));
    }

    /**
     * 检索部署方式返回添加产品选项
     *
     * @param basePageRequest basePageRequest
     */
    @ActionApi(summary = "检索部署方式返回添加产品选项", remark = "检索部署方式返回添加产品选项")
    @Params({
            @Param(name = "deploy", remark = "检索部署方式返回添加产品选项", dataType = "int", required = true)
    })
    public void queryDeploy(BasePageRequest<T> basePageRequest) {
        renderJson(combinationService.queryDeploy(basePageRequest));
    }

    /**
     * 检索创建人添加对应产品
     *
     * @param basePageRequest basePageRequest
     */
    @ActionApi(summary = "检索创建人添加对应产品", remark = "检索创建人添加对应产品")
    @Params({
            @Param(name = "itemOption", remark = "产品选项[硬件 授权 本地部署授权 年费 人工费]", dataType = "int", required = true),
            @Param(name = "search", remark = "检索条件", dataType = "String")
    })
    public void queryProductList(BasePageRequest<T> basePageRequest
    ) {
        renderJson(combinationService.queryProductList(basePageRequest));
    }

    /**
     * 新增 / 编辑
     */
    @Permissions({"crm:quotation:save", "crm:quotation:update"})
    @ActionApi(summary = "添加/编辑产品", remark = "添加/编辑产品")
    @Params({
            @Param(name = "quotationNumber", remark = "报价单号", dataType = "String", required = true),
            @Param(name = "quotationDate", remark = "报价日期", dataType = "date", required = true),
            @Param(name = "validPeriod", remark = "有效期(天)", dataType = "int", required = true),
            @Param(name = "quotationItemId", remark = "部署方式id", dataType = "int", required = true),
            @Param(name = "customerName", remark = "客户名称", dataType = "String", required = true),
            @Param(name = "businessName", remark = "商机名称", dataType = "int", required = true),
            @Param(name = "summary", remark = "摘要", dataType = "String"),
            @Param(name = "customerInfo", remark = "客户联系信息", dataType = "String", required = true),
            @Param(name = "senderInfo", remark = "发件人信息", dataType = "String", required = true),
            @Param(name = "invoiceInfo", remark = "抬头信息", dataType = "String", required = true),
            @Param(name = "finalCostPriceTax", remark = "成本价", dataType = "decimal", required = true),
            @Param(name = "finalSettlementPriceTax", remark = "结算价", dataType = "decimal", required = true),
            @Param(name = "finalDirectorPriceTax", remark = "总监总价", dataType = "decimal", required = true),
            @Param(name = "finalSalesPriceTax", remark = "总价", dataType = "decimal", required = true),
            @Param(name = "remark", remark = "项目备注", dataType = "String"),
            @Param(name = "checkStatus", remark = "审核状态[1 保存草稿 2 待审核 ]", dataType = "int", required = true),
            @Param(name = "materialOption", remark = "产品选项[1 硬件 2 授权 3 本地部署授权 4 年费 5 人工费]", dataType = "int", required = true),
            @Param(name = "productId", remark = "产品id", dataType = "int", required = true),
            @Param(name = "productPrice", remark = "产品单价", dataType = "decimal", required = true),
            @Param(name = "productNumber", remark = "产品数量", dataType = "int", required = true),
            @Param(name = "productRate", remark = "增值税", dataType = "int", required = true),
            @Param(name = "totalPrice", remark = "总价", dataType = "decimal"),
            @Param(name = "productModel", remark = "产品型号", dataType = "String"),
            @Param(name = "remark", remark = "备注", dataType = "String"),
            @Param(name = "说明", remark = "以上字段请参考前端产品添加进行传参", dataType = "String")
    })
    public void saveOrUpdate() {
        String data = getRawData();
        JSONObject jsonObject = JSON.parseObject(data);
        renderJson(combinationService.saveOrUpdate(jsonObject));
    }

    /**
     * 检索报价详情
     *
     * @param basePageRequest basePageRequest
     */
    @Permissions({"crm:quotation:read"})
    @ActionApi(summary = "检索报价详情", remark = "检索报价详情")
    @Param(name = "quotationId", remark = "根据id检索详情", dataType = "int", required = true)
    public void queryById(BasePageRequest<T> basePageRequest) {
        renderJson(combinationService.queryDetails(basePageRequest));
    }


    /**
     * 逻辑删除
     *
     * @param quotationIds quotationIds
     */
    @Permissions({"crm:quotation:delete"})
    @ActionApi(summary = "逻辑删除", remark = "逻辑删除")
    @Param(name = "quotationIds", remark = "根据id删除数据", dataType = "String", required = true)
    public void delQuotationByIds(@Para("quotationIds") String quotationIds) {
        renderJson(combinationService.delQuotationByIds(quotationIds));
    }

    /**
     * 根据id检索产品（物料）列表
     *
     * @param quotationId quotationId
     */
    @ActionApi(summary = "根据id检索产品（物料）列表", remark = "根据id检索产品（物料）列表")
    @Param(name = "quotationId", remark = "根据id检索产品（物料）列表", dataType = "int", required = true)
    public void queryMaterialList(@Para("quotationId") int quotationId) {
        renderJson(combinationService.queryMaterialList(quotationId));
    }


    @ActionApi(summary = "根据businessId检索合同对应的报价清单", remark = "根据businessId检索合同对应的报价清单")
    @Params({
            @Param(name = "businessId", remark = "根据businessId检索合同对应的报价清单", dataType = "int", required = true),
            @Param(name = "isValid", remark = "isValid为true时，检索审核通过的数据", dataType = "boolean", required = true),
            @Param(name = "search", remark = "检索条件", dataType = "String")
    })
    public void queryByBusinessIdForQuotationList(
            @Para("businessId") int businessId,
            @Para("isValid") boolean isValid,
            @Para("search") String search
    ) {
        renderJson(combinationService.queryByBusinessIdForQuotationList(businessId, isValid, search));
    }

}
